#!/usr/bin/perl -wT

require 5.001;
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);

# my $logfile = '/org/lists.debian.org/spam-log/spam-report.log';
my $logfile = '/tmp/spam-report.log';

umask 002;

print header;

my $listname;
my $date;
my $msg;
my $nominated;

if (param()) {
  if (param('listname') =~ /^([0-9a-z\-]+)$/) {
    $listname = $1;
  }
  if (param('date') =~ /^(\d{4}\/\d{2})$/) {
    $date = $1;
  }
  if (param('msg') =~ /^(msg\d+\.html)$/) {
    $msg = $1;
  }
  if (param('nominate')) {
    $nominated = 1;
  }

  if ($listname and $date and $msg) {
    if ($nominated) {
      print p(strong("reported as Spam"));
    } else {
      print start_form;
      print hidden('listname', $listname);
      print hidden('msg', $msg);
      print hidden('date', $date);
      print submit('nominate', 'Report as Spam');
      print end_form;
    }
  } else {
    print p("called incorrectly");
  }
} else {
  print p("called incorrectly");
}

if (0) {
    # log all connections that present sane input
    open (LOG, ">>$logfile") ;
    my $time = time;
    print LOG "time: $time; ";
    #if (exists $ENV{'REMOTE_HOST'}) {
    #  print LOG "host: $ENV{'REMOTE_HOST'}; addr: $ENV{'REMOTE_ADDR'} sub_cnt: $#subscribe unsub_cnt: $#unsubscribe: email: $user_email\n";
    #} else {
    print LOG "addr: $ENV{'REMOTE_ADDR'}; list: ".param('listname').", date: ".param('date').", msg: ".param('msg')."\n";
    #}
    close LOG;
    my $message = <<SUCCESS
<h2>Report accepted</h2>
<p>
Thank you for reporting this email as a potential spam message. Scores are reviewed regularly, and are used to mark emails as spam.
</p>
SUCCESS
;
    my $title = 'Email status recorded';
}
else {
    my $message = <<ERRMSG
<h2>An error occurred</h2>
<p>
Unfortunately, your report could not be recorded. Please try again or contact the listmasters.
</p>
ERRMSG
;
}
#print start_html(-title=>$title);

print end_html;
exit 0;
